package com.yueka.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yueka.mapper.CourseMapper;
import com.yueka.mapper.CourseRecordMapper;
import com.yueka.mapper.CourseStudentMapper;
import com.yueka.pojo.PageResult;
import com.yueka.pojo.course.Course;
import com.yueka.pojo.course.CourseQueryParam;
import com.yueka.service.CourseService;
import com.yueka.utils.CourseConflictDetector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseStudentMapper courseStudentMapper;
    @Autowired
    private CourseRecordMapper courseRecordMapper;

    @Override
    @Transactional
    public PageResult<Course> list(CourseQueryParam courseQueryParam) {
        PageHelper.startPage(courseQueryParam.getPage(),courseQueryParam.getPageSize());
        List<Course> list = courseMapper.list(courseQueryParam);
        list.forEach(c -> c.setStudentIds(courseStudentMapper.getStudentIds(c.getId())));
        Page<Course> p = (Page<Course>) list;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        courseMapper.delete(id);
        courseStudentMapper.deleteByCourseId(id);
        courseRecordMapper.delete(id);
    }

    @Override
    @Transactional
    public void insert(Course course) {
//        List<Course> courseList = CourseConflictDetector.detectConflicts(course, courseMapper.timeRepeats(course));
//        if (!courseList.isEmpty()) {
//            throw new RuntimeException(CourseConflictDetector.message);
//        }
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
        if ( course.getStudentIds() != null && !course.getStudentIds().isEmpty()) {
            courseStudentMapper.insert(course.getId(), course.getStudentIds());
        }
    }

    @Override
    @Transactional
    public void update(Course course) {
//        List<Course> courseList = CourseConflictDetector.detectConflicts(course, courseMapper.timeRepeats(course));
//        if (!courseList.isEmpty()) {
//            throw new RuntimeException(CourseConflictDetector.message);
//        }
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
        courseStudentMapper.deleteByCourseId(course.getId());
        if ( course.getStudentIds() != null && !course.getStudentIds().isEmpty()) {
            courseStudentMapper.insert(course.getId(), course.getStudentIds());
        }
    }

    @Override
    public List<Course> byStudentId(Integer studentId) {
        return courseMapper.byStudentId(studentId);
    }

    @Override
    public Integer getallTotalLessons(Integer studentId) {
        return courseMapper.getallTotalLessons(studentId);
    }

    // 字符串转 LocalDateTime
    public LocalDateTime stringToLocalDateTime(String dateString) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateString, formatter);
    }


    public LocalDateTime setTime(LocalDateTime dateTime, String timeString) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        try {
            LocalTime time = LocalTime.parse(timeString, timeFormatter);
            return dateTime.withHour(time.getHour())
                    .withMinute(time.getMinute())
                    .withSecond(time.getSecond());
        } catch (DateTimeParseException e) {
            System.out.println("Invalid time format: " + timeString);
            return dateTime; // 或者你可以选择抛出异常，取决于你的需求
        }
    }
}
